package net.ib.mtalk.util;

import net.ib.mtalk.R;
import net.ib.mtalk.global.GlobalPreferences;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LockActivity extends Activity 
{
	private EditText mPW1;
	private EditText mPW2;
	private EditText mPW3;
	private EditText mPW4;
	private Button mBtn;
	private String mType;
	private TextView mTextView;
	
	private Context mContext;
	
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lock);
		mContext = this;
		mType = getIntent().getStringExtra("type");
		if(mType == null && GlobalPreferences.getLockPassWord() == null)
		{
			finish();
			return;
		}
		initView();
	}
	
	private void initView()
	{
		mPW1 = (EditText)findViewById(R.id.pw_1);
		mPW1.addTextChangedListener(new TextWatcher(){
			public void afterTextChanged(Editable s) 
			{
				
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) 
			{
				
			}

			public void onTextChanged(CharSequence s, int start, int before,
					final int count) 
			{
				mPW2.post(new Runnable(){
					public void run() {
						if(count > 0)
							mPW2.requestFocus();
					}});
				
			}});
		
		mPW2 = (EditText)findViewById(R.id.pw_2);
		mPW2.addTextChangedListener(new TextWatcher(){
			public void afterTextChanged(Editable s) 
			{
				
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) 
			{
				
			}

			public void onTextChanged(CharSequence s, int start, int before,
					final int count) 
			{
				mPW3.post(new Runnable(){
					public void run() {
						if(count > 0)
							mPW3.requestFocus();
					}});
			}});
		
		mPW3 = (EditText)findViewById(R.id.pw_3);
		mPW3.addTextChangedListener(new TextWatcher(){
			public void afterTextChanged(Editable s) 
			{
				
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) 
			{
				
			}

			public void onTextChanged(CharSequence s, int start, int before,
					final int count) 
			{
				mPW4.post(new Runnable(){
					public void run() {
						if(count > 0)
							mPW4.requestFocus();
					}});
			}});
		
		mPW4 = (EditText)findViewById(R.id.pw_4);
		mPW4.addTextChangedListener(new TextWatcher(){
			public void afterTextChanged(Editable s) 
			{
				
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) 
			{
				
			}

			public void onTextChanged(CharSequence s, int start, int before,
					final int count) 
			{
				mPW1.post(new Runnable(){
					public void run() {
						if(count > 0)
							mPW1.requestFocus();
					}});
			}});
		
		mBtn = (Button)findViewById(R.id.pw_ok);
		mBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) 
			{
				StringBuilder sb = new StringBuilder();
				sb.append(mPW1.getText().toString()).append(mPW2.getText().toString()).append(mPW3.getText().toString()).append(mPW4.getText().toString());
				
				if(sb.length() < 4)
				{
					Toast.makeText(mContext, "Input Password!!", Toast.LENGTH_SHORT).show();
					return;
				}
				
				if(mType == null)
				{
					if(GlobalPreferences.getLockPassWord().equals(sb.toString()))
					{
						GlobalPreferences.setLock(false);
						finish();
					}
					else
					{
						new AlertDialog.Builder(mContext).setMessage(getString(R.string.setting_profile_lock_wrong_msg))
							.setNegativeButton(R.string.done, new OnClickListener() {
								public void onClick(DialogInterface dialog, int which) {
									dialog.dismiss();
								}
							}).create().show();
					}
				}
				else
				{
					
					if(mType.equals("lock"))
					{
						GlobalPreferences.setLockPassWord(sb.toString());
						GlobalPreferences.setBooleanLockChk(true);
					}
					else
					{
						if(GlobalPreferences.getLockPassWord().equals(sb.toString()))
							GlobalPreferences.setBooleanLockChk(false);
						else
						{
							new AlertDialog.Builder(mContext).setMessage(getString(R.string.setting_profile_lock_wrong_msg))
							.setNegativeButton(R.string.done, new OnClickListener() {
								public void onClick(DialogInterface dialog, int which) {
									dialog.dismiss();
								}
							}).create().show();
							return;
						}
					}
						
					GlobalPreferences.setLock(false);
					finish();
				}
					
			}
		});
		
		mTextView = (TextView)findViewById(R.id.pw_text);
		if(mType == null || mType.equals("unlock"))
			mTextView.setText("Input Password");
		else
			mTextView.setText("Password Setting");
	}
	
	public void onResume()
	{
		super.onResume();
		if(mType == null && !GlobalPreferences.getLock())
		{
			finish();
		}
	}
	
	public void onBackPressed()
	{
		if(mType != null)
			super.onBackPressed();
	}
}
